package com.example.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Queue;

@Component
public class QueueProducer {

    private JmsMessagingTemplate jmsMessagingTemplate;
    private Queue testQueue;

    @Autowired
    public void setJmsMessagingTemplate(JmsMessagingTemplate jmsMessagingTemplate) {
        this.jmsMessagingTemplate = jmsMessagingTemplate;
    }

    @Autowired
    public void setTestQueue(Queue testQueue) {
        this.testQueue = testQueue;
    }

    public void send(String message){
        try {
            System.out.println("往" + testQueue.getQueueName() + "中发送消息");
        } catch (JMSException e) {
            e.printStackTrace();
        }
        jmsMessagingTemplate.convertAndSend(testQueue, message);
    }

    // 配合定时任务使用
    // fixedDelay表示上一次执行完毕时间点之后多长时间再执行
    @Scheduled(fixedDelay = 100)
    public void autoSend(){
        send("这是queue消息");
    }
}
